from app.celery import logger, celery_app

from app.db.models.tasks import Task


@celery_app.task(acks_late=True, name="error:do_delete_task")
def do_delete_task(task_session_id=None, task_id=None):
    task = None
    msg = "Delete task"

    if task_session_id:
        task = Task.objects(taskSessionId=task_session_id)
        msg = f"{msg} taskSessionId: {task_session_id}"
    if task_id:
        task = Task.objects(pk=task_id)
        msg = f"{msg} taskId: {task_id}"
    if not task:
        return
    task.delete()
    logger.info(msg)


def do_rollback_task_progress(task_id):
    Task.objects(pk=task_id).update(status='active', progress=98)
    logger.info(f"Rollback task {task_id}'s progress.")
